#include "task_set.hpp"

#include <cassert>

#include "scheduler.hpp"

void ResourceGroup::_set_context(Scheduler *scheduler, size_t slot_index) {
  _scheduler = scheduler;
  _slot_index = slot_index;
  assert(_task_sets.size() >= 1);
  _task_sets[0]->_init_task_set(_get_cpu_worker_num());
}

void ResourceGroup::_next_task_set() {
  // BUG: broadcast next for gpu do not initialize pass value
  if (_current_task_set_index + 1 >= _task_sets.size()) {  // no task set
    _finish.set_value(true);
    _scheduler->reset_resource_group(_slot_index);
  } else {
    _task_sets[_current_task_set_index + 1]->_init_task_set(
        _get_cpu_worker_num());
    _scheduler->_broadcast_active(
        _slot_index, _task_sets[_current_task_set_index + 1]->is_gpu());
  }
  _current_task_set_index.fetch_add(1);
}

void ResourceGroup::_collect_analyse(size_t worker_id,
                                     std::tuple<size_t, double, double> info) {
  _scheduler->collect_analyse(worker_id, info);
}

size_t ResourceGroup::_get_cpu_worker_num() {
  return _scheduler->get_cpu_worker_num();
}

void ResourceGroup::_broadcast_finish(bool is_gpu) {
  _scheduler->_broadcast_finish(_slot_index, is_gpu);
}
